require(["common", "queryString", "coAjax", "hash_routie", "href"], function(jSouper, QueryString, coAjax, hash_routie, href) {
	hash_routie.on('appraise', function() {
		var queryString = new QueryString(location.hash);
		var oid = queryString.get("oid");
		if (!oid) {
			alert("error", "无效的订单编号");
			href.jump("personal-beta.html#");
		}
		coAjax.get(appConfig.user.ach_by_id_with_eval + oid, function(result) {
			var ach = result.result;
			jSouper.forEach(ach.info.goods_info, function(goods_info) {
				//初始化评价等级为10
				goods_info._eval_star_num = 10
			});
			App.set("user.appraise", ach);
		}, function(errorCode) {
			console.log("errorCode:", errorCode);
			alert("error", "无效的订单编号");
		});
	});
	//格式化评价等级为整数
	App.set('$Event.eval.checkStar', function(e, vm) {
		var star_num = parseInt(this.value);
		if (star_num > 10) {
			vm.set('_eval_star_num', 10);
		} else if (star_num < 0) {
			vm.set('_eval_star_num', 0);
		} else if (isNaN(star_num)) {
			vm.set('_eval_star_num', 10);
		} else {
			vm.set('_eval_star_num', star_num);
		}
	});
	//提交评价
	App.set("$Event.eval.submitEval", function(e, vm) {
		var evaluationData = [];
		jSouper.forEach(App.get("user.appraise.info.goods_info"), function(goods_info) {
			evaluationData.push({
				goods_id: goods_info.goods._id,
				star_num: ~~goods_info._eval_star_num || 10,
				isAny: !!goods_info._eval_isAny,
				content: goods_info._eval_content
			});
		});
		// console.log(evaluationData);
		// var len = evaluationData.length;
		// for (var i = 0; i < len; i++) {
		// 	if (!$.trim(evaluationData[i].$Cache.eval.content)) {
		// 		alert('warn', '您尚有评价内容未填写，请检查！！！');
		// 		return;
		// 	}
		// };
		// for (var i = 0; i < len; i++) {
		// 	coAjax.post(appConfig.user.add_eval + evaluationData[i].goods._id, {
		// 		star_num: evaluationData[i].$Cache.eval.star_num || 10,
		// 		content: evaluationData[i].$Cache.eval.content
		// 	}, function(result) {
		// 		// alert('评价成功！');
		// 	});
		// };

		var url;
		var state = App.get("user.appraise.info.state");
		if (state == appConfig.ACH_STATE["ol交易完成"]) {
			url = appConfig.user["ach_ol交易完成_to_ol已评价"]
		} else if (state == appConfig.ACH_STATE["交易完成"]) {
			url = appConfig.user["ach_交易完成_to_已评价"]
		} else {
			alert("error", "错误的订单状态")
			return;
		}
		coAjax.put(url + App.get("user.appraise._id"), {
			evaluations: evaluationData
		}, function(result) {
			alert("评价完成");
			console.log(result.result);
			App.set("user.appraise.info.state", result.result);
			hash_routie.emit("appraise");
		});
	});
})